﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ThreeByte.ActiveDeck.Config;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Excel = Microsoft.Office.Interop.Excel;
using ThreeByte.ActiveDeck.Data.UserResponse;


namespace ThreeByte.ActiveDeck.Controls.UserResponse
{
    /// <summary>
    /// Interaction logic for MultipleChoiceResponseConfiguration.xaml
    /// </summary>
    public partial class MultipleChoiceResponseConfiguration : UserControl, IUserResponseConfigurationControl
    {

        private MultipleChoiceUserData mData;
        private MultipleChoiceConfig mConfig;

        public MultipleChoiceResponseConfiguration(PageConfig config) {
            InitializeComponent();

            DataContext = config;

            mConfig = (MultipleChoiceConfig)config;
            mData = ((MultipleChoiceConfig)config).InitUserData();
        }



        public PowerPoint.Shape InsertUserResponseComponent(PowerPoint.Slide slide) {

            //Alternative OLE Test code - For investigation
            //Slide currentSlide = Globals.ThisAddIn.Application.ActiveWindow.View.Slide as Slide;
            //Shape theNewShape = currentSlide.Shapes.AddOLEObject(0f, 0f, -1f, -1f, null, @"C:\Users\chris\Desktop\Book1.xlsx!Sheet1![Book1.xlsx]Sheet1 Chart 1",
            //            Microsoft.Office.Core.MsoTriState.msoFalse, null, 0, null, Microsoft.Office.Core.MsoTriState.msoTrue);
            //theNewShape.Name = "ActiveDeck_OLEChart";


            //TODO: Insert centered, at half the height/width of the slide
            PowerPoint.Shape theShape = slide.Shapes.AddChart(Microsoft.Office.Core.XlChartType.xl3DBarClustered);
            //Store Section/Page info to the shape
            theShape.Name = @"ActiveDeck_Chart";
            theShape.Tags.Add("Section", mConfig.ParentSlideConfig.SlideId.ToString());
            theShape.Tags.Add("Page", mConfig.PageId.ToString()); //TODO: Get the page business figured out
            theShape.Tags.Add("ConfigType", typeof(MultipleChoiceConfig).ToString()); //TODO: Get the page business figured out


            PowerPoint.Chart theChart = theShape.Chart;
            PowerPoint.ChartData theChartData = theChart.ChartData;

            Excel.Workbook theWorkbook = (Excel.Workbook)(theChartData.Workbook);
            Excel.Worksheet theWorksheet = theWorkbook.Worksheets[1];

            //TODO: Figure out what this actually does
            //gWorkSheet.Range("Table1[[#Headers],[Series 1]]").Value = "Items"
            //Clear out the current sheet data
            theWorksheet.Range["a2", "k20"].Value = null;
            theWorksheet.Range["c1", "k20"].Value = null;

            //Set the headers
            theWorksheet.Range["a1"].Value = @"Answers";
            theWorksheet.Range["b1"].Value = @"User Responses";

            //Fill in the categories
            int i = 1;
            foreach (Answer a in mConfig.Answers){
                string cell = string.Format("a{0}", ++i);
                theWorksheet.Range[cell].Value = a.Text;
            }

            //Fill in the (sample) responses
            mData.UseSampleData(true);

            foreach (MultipleChoiceAnswerData m in mData.Answers) {
                string cell = string.Format("b{0}", m.Value + 1);
                theWorksheet.Range[cell].Value = m.UserResponses;
            }

            
            //Resize the table
            string extentCell = string.Format("B{0}", i);
            theWorksheet.ListObjects["Table1"].Resize(theWorksheet.Range["A1", extentCell]);

            //Set the chart title
            theChart.ChartTitle.Text = mConfig.Question;
            theChart.Legend.Delete();

            //Minimize
            theWorkbook.Application.WindowState = Excel.XlWindowState.xlMinimized;
            return theShape;
        }


    }
}
